$(function () {
    "use strict";

    if (!CSS.supports("clip-path", "circle(120px at center)") && !CSS.supports("-webkit-clip-path", "circle(120px at center)")) {
        $("#stage").html('<img src="screenshot-1.png"/>');
        return;
    }

    let apiURL = "https://live2d.fghrsh.net/api", state = 0,
        modelId = localStorage.getItem("modelId"),
        modelTexturesId = localStorage.getItem("modelTexturesId");
    if (modelId == null) {
        modelId = 1;
        modelTexturesId = 53;
    }
    loadModel(modelId, modelTexturesId);

    function loadModel(modelId, modelTexturesId) {
        localStorage.setItem("modelId", modelId);
        if (modelTexturesId === undefined) modelTexturesId = 0;
        localStorage.setItem("modelTexturesId", modelTexturesId);
        loadlive2d("live2d", `${apiURL}/get/?id=${modelId}-${modelTexturesId}`, null);
        console.log("live2d", `模型 ${modelId}-${modelTexturesId} 加载完成`);
        setTimeout(function () {
            $("#cover").css("bottom", "80%");
            state = 2;
        }, 2000);
    }

    function loadRandModel() {
        let modelId = localStorage.getItem("modelId"),
            modelTexturesId = localStorage.getItem("modelTexturesId");
        $.ajax({
            cache: false,
            url: `${apiURL}/rand_textures/?id=${modelId}-${modelTexturesId}`,
            dataType: "json",
            success: function (result) {
                loadModel(modelId, result.textures["id"]);
                setTimeout(function () {
                    state = 2;
                    $("#cover").css("bottom", "80%");
                    $("#refresh").attr("href", "javascript:refresh()");
                }, 1000);
            }
        });
    }

    function loadOtherModel() {
        let modelId = localStorage.getItem("modelId");
        $.ajax({
            cache: false,
            url: `${apiURL}/switch/?id=${modelId}`,
            dataType: "json",
            success: function (result) {
                loadModel(result.model["id"]);
            }
        });
    }

    window.info = function () {
        //Your code here...
        $.getJSON("https://v1.hitokoto.cn/", function (data) {
            alert("『" + data.hitokoto + "』——" + data.from);
        });
    };
    window.refresh = function () {
        state = 0;
        $("#cover").css("bottom", "10%");
        $("#refresh").attr("href", "javascript:void(0)");
        setTimeout(loadRandModel, 1000);
    };
    $("#handle").click(function () {
        if (state == 1) {
            state = 2;
            $("#cover").css("bottom", "80%");
        }
        else if (state == 2) {
            state = 1;
            $("#cover").css("bottom", "20%");
        }
    });

    $("input[type=password]").focus(function () {
        if (state == 2) {
            state = 1;
            $("#cover").css("bottom", "20%");
        }
    }).blur(function () {
        if (state == 1) {
            state = 2;
            $("#cover").css("bottom", "80%");
        }
    });
});